iOS 设置.gitignore文件 忽略.DS_Store 、xcuserstate等文件

开发项目时会有一些文件一直自动更新但是不必提交的文件如版本管理文件,如果不忽略提交代码时会遇到下方报错。比如xcuserstate文件,这个文件会实时更新不管有没有更改过代码等,此文件保存的是项目的状态之类的,不需要提交。

1
2
3
The working copy “项目名称” has uncommitted changes.

Commit or discard the changes and try again.

此时需要配置git的.gitignore文件忽略掉不需要提交的文件。

1. 设置项目中的.gitignore文件。

.gitignore文件是隐藏文件需要显示隐藏文件才能看到,终端执行下方命令可以显示隐藏文件。有没有大神知道显示隐藏文件的快捷键。

1
defaults write com.apple.finder AppleShowAllFiles -boolean true ; killall Finder

创建.gitignore文件,首先进入到项目目录下执行下面终端命令

1
touch .gitignore

.gitignore
打开编辑忽略配置文件

1
open .gitignore

直接通过命令打开或者找到此文件直接双击打开用文本编辑器或者其他编辑器都可以,然后把下面代码(需要忽略的文件)粘贴进去保存提交到远端就OK了。以后提交就不会出现上面的问题。
或者点击 GitHub Objective-C.gitignore项目地址 objc-c项目在GitHub上.gitignore地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 # ---> Objective-C
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

##file
*.app.dSYM.zip
.DS_Store

## Build generated
build/
DerivedData

## .DS_store
*.DS_Store
.DS_Store
.DS_Store?
*.swp
*.swo

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata

## Other
*.xccheckout
*.moved-aside
*.xcuserstate
*.xcscmblueprint

## Obj-C/Swift specific
*.hmap
*.ipa

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# http://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
#Pods/

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build

2. 在SourceTree中设置.gitignore文件。

SourceTree的偏好设置里面有一个可以设置.gitignore文件选项,可以直接在里面设置忽略文件,优点是只要是使用SourceTree提交代码就可以自动忽略,不用每个项目在设置忽略文件。
SourceTree

写在最后

Git忽略规则(.gitignore配置)不生效原因和解决

  • 第一种方法:
    .gitignore中已经标明忽略的文件目录下的文件,git push的时候还会出现在push的目录中,或者用git status查看状态,想要忽略的文件还是显示被追踪状态。
    原因是因为在git忽略目录中,新建的文件在git中会有缓存,如果某些文件已经被纳入了版本管理中,就算是在.gitignore中已经声明了忽略路径也是不起作用的,
    这时候我们就应该先把本地缓存删除,然后再进行git的提交,这样就不会出现忽略的文件了。

解决方法: 进入项目对应路径清除git本地缓存(改变成未track状态),然后再提交:

1
2
3
4
git rm -r --cached .
git add .
git commit -m 'update .gitignore'
git push -u origin master

需要特别注意的是:

  1. gitignore只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。
  2. 想要.gitignore起作用,必须要在这些文件不在暂存区中才可以,.gitignore文件只是忽略没有被staged(cached)文件,
    对于已经被staged文件,加入ignore文件时一定要先从staged移除,才可以忽略。
  • 第二种方法:(推荐)
    在每个clone下来的仓库中手动设置不要检查特定文件的更改情况。
    1
    git update-index --assume-unchanged PATH  //在PATH处输入要忽略的文件
-------------本文结束感谢您的阅读-------------

本文标题:iOS 设置.gitignore文件 忽略.DS_Store 、xcuserstate等文件

文章作者:leon

发布时间:2018年06月22日 - 00:06

最后更新:2019年06月25日 - 16:06

原始链接:https://huanghaipo.github.io/2018/06/22/iOS 设置.gitignore文件 忽略.DS_Store 、xcuserstate等文件/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

坚持原创技术分享,您的支持将鼓励我继续创作!